package cn.mldn.lzs.dao.proxy;

import cn.mldn.lzs.dao.IEmpDAO;
import cn.mldn.lzs.dao.impl.EmpDAOImpl;
import cn.mldn.lzs.vo.Emp;

import java.util.List;

import cn.mldn.lzs.dbc.DatabaseConnection;

/**
 * Created by Administrator on 2017/4/3.
 */
public class EmpDAOProxy implements IEmpDAO {
    private DatabaseConnection dbc = null;
    private IEmpDAO dao = null;
    public EmpDAOProxy() throws Exception{
        this.dbc = new DatabaseConnection();    //进行了数据库连接
        this.dao = new EmpDAOImpl(this.dbc.getConnection());    //取得数据库连接对象，并传入实现类进行设置，返回能够实际操作的一个控制型对象
    }


    @Override
    public boolean doCreate(Emp emp) throws Exception {
        boolean flag = false;
        try {
            if (this.dao.findById(emp.getEmpno()) == null){
                flag = this.dao.doCreate(emp);
            }
        }catch (Exception e){
            throw e;
        }finally {
            this.dbc.close();
        }

        return flag;
    }

    @Override
    public List<Emp> findAll(String keyWord) throws Exception {
        List<Emp> all = null;
        try {
            all = this.dao.findAll(keyWord);
        }catch (Exception e){
            throw e;
        }finally {
            this.dbc.close();
        }

        return all;
    }

    @Override
    public Emp findById(int empno) throws Exception {
        Emp emp = null;
        try {
            this.dao.findById(empno);
        }catch (Exception e){
            throw e;
        }finally {
            this.dbc.close();
        }
        return emp;
    }
}
